<?php
	/**
	 * Class for extracting data from IEEE Xplore Digital Library
	 * 
	 * @author Dragan Milchevski, Evica Ilieva, Daniil Luzin
	 * @date 2012-06-27
	 */
	class IeeeApiImplDAO implements RepositoryDAO{

		/**
		 * Get Document Object by doi
		 * @author Dragan
		 * @param String $doi
		 */
		public function searchByDoi($doi){
			
			$request_url = str_replace('[QUERY]', 'doi='.urlencode($doi), RepositoryConnectionProperty::$_url);
			RepositoryConnectionProperty::$_url = $request_url;
			
			//Connect to the Repository
			$conn = new RepositoryConnection();
				
			//The connection returns an XML object
			$xml=$conn->getConnection();
			
						//Create empty objects for Document
			$document = new Document();
			
			//Populate the document with the data from the returned xml
			$document = $this->populateDocument($xml);
			
			//Return the Document Object
			return $document;
		}
		
		
		/**
		 * @author 
		 */
		public function findDoiByISSN($issn, $title){
		}
		
		/**
		 * Get Document Object by uid
		 * Evica Ilieva
		 * @param String $uid
		 */
		public function searchByUid($uid){
			$request_url = str_replace('[QUERY]', 'an='.$uid, RepositoryConnectionProperty::$_url);
			RepositoryConnectionProperty::$_url = $request_url;
				
			//Connect to the Repository
			$conn = new RepositoryConnection();
			
			//The connection returns an XML object
			$xml=$conn->getConnection();
				
			//Create empty objects for Document
			$document = new Document();
			
			//Populate the document with the data from the returned xml
			$document = $this->populateDocument($xml);
			
				
			//Return the Document Object
			return $document;
				
		}
		
		/**
		 * Get document by title using IEEE API
		 * 
		 * @author  Daniil Luzin
		 */
		public function searchByTitle($title){
			
			if (!empty($title)) $title = trim($title);
			
			$title_encoded = urlencode($title);
				
			// Daniil: we retreive 10 results and compare them
			$request_url = str_replace('[QUERY]', 'hc=25&ti='.$title_encoded, RepositoryConnectionProperty::$_url);
			RepositoryConnectionProperty::$_url = $request_url;
			
			//Connect to the Repository
			$conn = new RepositoryConnection();
			
			//The connection returns an XML object
			$xml=$conn->getConnection();
				
						//Create empty objects for Document
			$document = new Document();
			
			//Populate the document with the data from the returned xml
			$document = $this->populateDocument($xml);
						
			//Return the Document Object
			return $document;				

		}
		
		private function populateDocument($xml){
			
			//Create empty objects for Document, Metadata and File
			$document = new Document();
			$metadata = new Metadata();
			$file = new File();
			$keywords = array();
			//Populate the Metadata Object
			$metadata->setAuthors((string)$xml->document->authors);
			$metadata->setAffiliation((string)$xml->document->affiliations);
			$metadata->setTitle((string)$xml->document->title);
			$metadata->setJournal((string)$xml->document->pubtitle);
			$metadata->setVolume((string)$xml->document->volume);
			$metadata->setIssue((string)$xml->document->issue);
			$metadata->setPages((string)$xml->document->spage."-".(string)$xml->document->epage);
			$metadata->setAbstract((string)$xml->document->abstract);
			$metadata->setPublisher((string)$xml->document->publisher);
			$metadata->setReferenceType((string)$xml->document->pubtype);
			$metadata->setDoi((string)$xml->document->doi);
			$metadata->setUrl((string)$xml->document->mdurl);
			$metadata->setYear((string)$xml->document->py."-01-01"); //Check if py returns full date. For now the month and the day are hardcoded to be 01-01
			$metadata->setAutorsAscii(utf8_deaccent((string)$xml->document->authors));
			$metadata->setTitleAscii(utf8_deaccent((string)$xml->document->title));
			$metadata->setAbstractAscii(utf8_deaccent((string)$xml->document->abstract));
			
			//Populate the list of keywords
			$i=0;
			$tmp_array = array();
			if(isset($xml->document->controlledterms)){
				foreach($xml->document->controlledterms->term as $term){
			
					$keyword = new Keyword();
					$keyword->setKeyword((string)$term);
					$keyword->setSelected(true);
					$keywords[$i]=$keyword;
					$tmp_array[$i] = (string)$term;
					$i++;
				}
			}
			
			if(isset($xml->document->thesaurusterms)){
				foreach($xml->document->thesaurusterms->term as $term){
					$keyword = new Keyword();
					$keyword->setKeyword((string)$term);
					$keyword->setSelected(true);
			
					if(!in_array((string)$term, $tmp_array)){
						$keywords[$i]=$keyword;
					}
					$tmp_array[$i] = (string)$term;
					$i++;
				}
			}
				
			if(isset($xml->document->uncontrolledterms)){
				foreach($xml->document->uncontrolledterms->term as $term){
					$keyword = new Keyword();
					$keyword->setKeyword((string)$term);
					$keyword->setSelected(true);
			
					if(!in_array((string)$term, $tmp_array)){
						$keywords[$i]=$keyword;
					}
					$tmp_array[$i] = (string)$term;
					$i++;
				}
			}
			
			//print_r($keywords);
			$metadata->setKeywords($keywords);
			
			//Populate the File Object
			$file->setUrl((string)$xml->document->pdf);
			
			//Populate the Document Object
			$document->setUid("IEEE:".(string)$xml->document->arnumber);
			$document->setMetadata($metadata);
			$document->setFile($file);
			
			//Return the Document Object
			return $document;
		}
	
		/**
		 * Get document by $query using IEEE API
		 * 
		 * Search ALL searchable metadata 
		 * without specifying individual fields  
		 * 
		 * 
		 * @author  Daniil Luzin
		 */
		public function searchByAnyField($query){
			
			if (!empty($query)) $query = trim($query);
				
			$query_encoded = urlencode($query);
			
			//Query to IEEE
			$request_url = str_replace('[QUERY]', 'hc=1&querytext='.$query_encoded, RepositoryConnectionProperty::$_url);
			RepositoryConnectionProperty::$_url = $request_url;

			//Connect to the Repository
			$conn = new RepositoryConnection();
				
			//The connection returns an XML object
			$xml=$conn->getConnection();
			
			//Create empty objects for Document
			$document = new Document();
				
			//Populate the document with the data from the returned xml
			$document = $this->populateDocument($xml);
			
			//Return the Document Object
			return $document;
		}
		
	}
?>